package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.SelectPermissionPagerAdapter;
import com.wwwarehouse.usercenter.bean.authoritydistribute.DistributeResultBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.QuaryDistributePermissionErrorInfo;
import com.wwwarehouse.usercenter.bean.response.DistributePermissionArrayBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 分配权限->错误详情
 * Created by ShineF on 2017/9/26 0026.
 */

public class DistributePermissionErrorPagerFragment extends BaseFragment {
    ArrayList<DistributePermissionArrayBean> arrayList = new ArrayList<>();
    private int itemHeight = 50;
    //    private StateLayout mStateLayout;
    private CustomViewPagerInternal mCustomPager;
    private CirclePageIndicator mCirclePager;
    private List<Fragment> mFragmentList;
    private int mHeight = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_distribute_error_pager, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //        mStateLayout = $(R.id.state_layout);
        mCustomPager = $(R.id.custom_pager);
        mCirclePager = $(R.id.pager_indicator);
        mFragmentList = new ArrayList<>();
        mCustomPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mCustomPager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setHeight(mCustomPager.getMeasuredHeight());
            }
        });
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof DistributePermissionErrorPagerFragment) {
            mActivity.setTitle(R.string.select_distribute_permission_title);
        }
    }

    @Override
    public void requestDatas() {

    }

    public void setHeight(int height) {
        this.mHeight = height;
                Bundle bundle = getArguments();
        if (null != bundle) {
            int type = bundle.getInt("type");
            DistributeResultBean bean = bundle.getParcelable("resultBean");
            if (null != bean) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", "1");
        map.put("size", "0");
//        map.put("processId", "12096900000035199");
//        map.put("type", "0");
                map.put("processId", bean.getProcessId());
                map.put("type", type);
        loadData(UserCenterConstant.QUERY_DISTRIBUTE_ERROR_INFO, map, 1);
            }
        }
    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if ("0".equals(commonClass.getCode())) {
                    QuaryDistributePermissionErrorInfo bean = JSON.parseObject(commonClass.getData().toString(), QuaryDistributePermissionErrorInfo.class);
                    if (null == bean.getList() || 0 == bean.getList().size()) {
//                        mStateLayout.showSystemView(getString(R.string.no_request_data), true);
                    } else {
                        setData(bean);
                    }
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }

    public void setData(QuaryDistributePermissionErrorInfo bean) {
        mFragmentList.clear();
        arrayList.clear();
        if (null != bean) {
            List<QuaryDistributePermissionErrorInfo.ListBean> list = bean.getList();
            if (mHeight != 0 && null != list && list.size() != 0) {
                for (QuaryDistributePermissionErrorInfo.ListBean listBean : list) {
                    arrayList.add(new DistributePermissionArrayBean(listBean.getUserName()));
                    for (QuaryDistributePermissionErrorInfo.ListBean.FailDetailDTOSBean failDetailDTOSBean : listBean.getFailDetailDTOS()) {
                        arrayList.add(new DistributePermissionArrayBean(failDetailDTOSBean.getDetailMsg(), failDetailDTOSBean.getAuthName()));
                    }
                }
                itemHeight = ConvertUtils.dip2px(mActivity, itemHeight);
                int total = arrayList.size() - 1;
                int size = mHeight / itemHeight;
                int page = total % size == 0 ? total / size : (total / size + 1);
                for (int i = 0; i < page; i++) {
                    DistributePermissionErrorDetailsFragment fragment = new DistributePermissionErrorDetailsFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("page", i);
                    bundle.putInt("height", itemHeight);
                    bundle.putInt("size", size);
                    bundle.putParcelableArrayList("data", arrayList);
                    fragment.setArguments(bundle);
                    mFragmentList.add(fragment);
                }
                if (isAdded()) {
                    SelectPermissionPagerAdapter adapter = new SelectPermissionPagerAdapter(getChildFragmentManager(), mFragmentList);
                    mCustomPager.setAdapter(adapter);
                    mCirclePager.setViewPager(mCustomPager);
                    mCustomPager.setOffscreenPageLimit(5);
//            mStateLayout.showContentView();
                }
            }

        }
    }
}
